class_name CombatTracker
extends Resource

# 战斗追踪器 - 专门追踪战斗相关的统计数据
# 职责：记录连击、暴击、格挡等战斗统计

signal combat_event(event_type: String, data: Dictionary)

# 战斗统计数据
var consecutive_hits: int = 0
var consecutive_blocks: int = 0
var max_single_damage: int = 0
var total_damage_dealt: int = 0
var total_damage_taken: int = 0
var critical_hits: int = 0
var perfect_blocks: int = 0
var combo_counter: int = 0

# 战斗事件历史
var combat_history: Array[Dictionary] = []
var max_history_size: int = 50

func _init():
	reset_turn_data()

# 重置回合数据
func reset_turn_data() -> void:
	consecutive_hits = 0
	consecutive_blocks = 0
	max_single_damage = 0
	total_damage_dealt = 0
	total_damage_taken = 0
	critical_hits = 0
	perfect_blocks = 0
	combo_counter = 0
	combat_history.clear()

# 追踪生命值事件
func track_health_event(old_health: int, new_health: int) -> void:
	var event_data = {
		"type": "health_change",
		"old_value": old_health,
		"new_value": new_health,
		"change": new_health - old_health,
		"timestamp": Time.get_unix_time_from_system()
	}
	
	_add_to_history(event_data)
	
	if new_health < old_health:
		total_damage_taken += old_health - new_health
		consecutive_blocks = 0  # 受伤中断连续格挡
		combat_event.emit("damage_taken", event_data)
	elif new_health > old_health:
		combat_event.emit("health_gained", event_data)

# 追踪护盾事件
func track_block_event(old_block: int, new_block: int) -> void:
	var event_data = {
		"type": "block_change",
		"old_value": old_block,
		"new_value": new_block,
		"change": new_block - old_block,
		"timestamp": Time.get_unix_time_from_system()
	}
	
	_add_to_history(event_data)
	
	if new_block > old_block:
		consecutive_blocks += 1
		if old_block == 0:  # 完美格挡（从0开始获得护盾）
			perfect_blocks += 1
		combat_event.emit("block_gained", event_data)
	elif new_block < old_block:
		combat_event.emit("block_lost", event_data)

# 追踪伤害事件
func track_damage_event(total_damage: int, damage_to_health: int, damage_to_block: int) -> void:
	var event_data = {
		"type": "damage_dealt",
		"total_damage": total_damage,
		"damage_to_health": damage_to_health,
		"damage_to_block": damage_to_block,
		"timestamp": Time.get_unix_time_from_system()
	}
	
	_add_to_history(event_data)
	
	if damage_to_health > 0:
		consecutive_hits += 1
		total_damage_dealt += damage_to_health
		if damage_to_health > max_single_damage:
			max_single_damage = damage_to_health
		
		# 检查是否为暴击（这里用简单的阈值判断，实际可能需要更复杂的逻辑）
		if damage_to_health >= 15:
			critical_hits += 1
			event_data["is_critical"] = true
		
		combo_counter += 1
		combat_event.emit("damage_dealt", event_data)
	else:
		consecutive_hits = 0  # 没有造成伤害，重置连击

# 添加事件到历史记录
func _add_to_history(event_data: Dictionary) -> void:
	combat_history.append(event_data)
	if combat_history.size() > max_history_size:
		combat_history.pop_front()

# 检查战斗条件
func check_combat_condition(condition_data: Dictionary) -> bool:
	var condition_type = condition_data.get("condition_type", "")
	var threshold = condition_data.get("threshold", 0)
	
	match condition_type:
		"consecutive_hits":
			return consecutive_hits >= threshold
		"consecutive_blocks":
			return consecutive_blocks >= threshold
		"max_single_damage":
			return max_single_damage >= threshold
		"total_damage_dealt":
			return total_damage_dealt >= threshold
		"total_damage_taken":
			return total_damage_taken >= threshold
		"critical_hits":
			return critical_hits >= threshold
		"perfect_blocks":
			return perfect_blocks >= threshold
		"combo_counter":
			return combo_counter >= threshold
		_:
			print("❌ 未知战斗条件类型: %s" % condition_type)
			return false

# 获取战斗统计
func get_combat_stats() -> Dictionary:
	return {
		"consecutive_hits": consecutive_hits,
		"consecutive_blocks": consecutive_blocks,
		"max_single_damage": max_single_damage,
		"total_damage_dealt": total_damage_dealt,
		"total_damage_taken": total_damage_taken,
		"critical_hits": critical_hits,
		"perfect_blocks": perfect_blocks,
		"combo_counter": combo_counter,
		"combat_history_size": combat_history.size()
	}

# 打印战斗统计
func print_combat_stats() -> void:
	print("  连续命中: %d" % consecutive_hits)
	print("  连续格挡: %d" % consecutive_blocks)
	print("  最大单次伤害: %d" % max_single_damage)
	print("  总造成伤害: %d" % total_damage_dealt)
	print("  总受到伤害: %d" % total_damage_taken)
	print("  暴击次数: %d" % critical_hits)
	print("  完美格挡: %d" % perfect_blocks)
	print("  连击计数: %d" % combo_counter) 
